//
//  TravelScene.cpp
//  SpaceTrader
//
//  Created by Brandon Chastain on 11/4/12.
//  Copyright (c) 2012 The Hex Pistols. All rights reserved.
//

#include "TravelScene.h"
#include "GuiTools.h"
#include "GuiButton.h"
#include "System.h"
#include <sstream>

TravelScene::TravelScene(Universe *universe) : travelManager(TravelManager()), universe(universe),
                                                    A(NULL), B(NULL), distanceMoved(0), distance(0) { }

state_t TravelScene::draw(SDL_Surface *screen, Player *player, int d)
{
    state_t nextState = TRAVEL, nextEvent = PLANET;
    SDL_Event event;
	distance = d;
	distanceMoved = 0;
    
	const int encounterRate = 15;
	
	//interpret as: 60% chance on event for trader, 40% for combat
	const int traderRate = 60;
	const int combatRate = 100;
	
    GuiTable table(1, 1, 0, 0, 20, 20, 20);
	GuiTable eventTable(2,1,20,100,20,25,20);
    std::ostringstream out;
    if (A != NULL && B != NULL){
        out << "Travelling from " << A->getName() << " to " << B->getName() << ". ";
        //distance = travelManager.getDistance(A, B, universe);
        //out << "Distance: " << distance;
    } else {
        nextState = PLANET;
    }
	
    const char* text[] = {out.str().c_str()};
    table.setContents(text);
	out.str("");
	out.flush();
	std::vector<std::string> lines;
	
    
    GuiButton OkButton(50, 200, "", "OK");
    int interval;
	while (distance > 0 && nextEvent == PLANET) {
		interval = (distance - 10 >= 0) ? 10 : distance;
		distance -= interval;
		distanceMoved += interval;
		//the distance check is to prevent "At 0 parsecs..." which does ot make sense
		if (distance > 0 && rand()%100 < encounterRate) {
			out << distance << " parsecs away from " << B->getName() << " you encounter a " ;
			lines.push_back(out.str());
			
			int r = rand()%100;
			if (r < traderRate) {
				nextEvent = MARKET;
				lines.push_back("travelling merchant who wishes to trade with you.");
			}
			else if (r < combatRate) {
				nextEvent = COMBAT;
				lines.push_back("pirate that begins to fire at you.");
			}
		}
	}
	if (nextEvent == PLANET) {
		out << "You arrive safely at " << B->getName() ;
		lines.push_back(out.str());
		lines.push_back(" ");
	}
	eventTable.setContents(lines);
    while (nextState == TRAVEL){
		
		
        while (SDL_PollEvent(&event)){
            if (event.type == SDL_QUIT){
                nextState = QUIT;
            }
            if (OkButton.handle_input(event) == CLICKED){
				player->travel(distanceMoved);
				nextState = nextEvent;
            }
        }
        
        SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00));
        OkButton.show(screen);
		eventTable.show(screen);
        table.show(screen);
        
        //Update the screen
		if ( SDL_Flip( screen ) == -1 ) {
			std::cerr << "Error Updating screen." << std::endl;
		}
    }
    return nextState;
}

void TravelScene::setA(Planet *planet)
{
    A = planet;
}

void TravelScene::setB(Planet *planet)
{
    B = planet;
}

int TravelScene::getDistanceMoved()
{
	return distanceMoved;
}